<script>
	// H5所需sdk  不用就删掉   package.json也删掉，不然会影响小程序的打包大小
	// #ifdef H5
	import TIM from 'tim-js-sdk/tim-js-friendship.js';
	import COS from 'cos-js-sdk-v5';
	// #endif
	// #ifdef MP-WEIXIN  || APP-PLUS
	import TIM from 'tim-wx-sdk/tim-wx-friendship.js';
	import COS from 'cos-wx-sdk-v5';
	import { genTestUserSig } from '@/im/debug/GenerateTestUserSig.js';
	// #endif
	
	import constant from '@/common/constant.js';
	import Vue from 'vue';

	export default {
		data() {
			return {
				versionName: "",
			};
		},
		globalData: {
			userInfo: null,
			userProfile: null,
			webviewUrl: '',
			headerHeight: 0,
			statusBarHeight: 0,
			openTicket: false,
			ticketTime: 0,
			ticketTotalTime: 180
		},
		onLaunch() {
			this.requestBaseUrl();
			Vue.prototype.TIM = TIM;

			const compensateHeight = 10; //非刘海屏状态栏高度偏低，补充10
			uni.getSystemInfo({
				success: (e) => {
					// 获取手机状态栏高度
					let statusBar = e.statusBarHeight
					let customBar
					// #ifndef MP
					customBar = statusBar + (e.platform == 'android' ? 50 : 45)
					// #endif
					// #ifdef MP-WEIXIN
					// 获取胶囊按钮的布局位置信息
					let menu = wx.getMenuButtonBoundingClientRect()
					// 导航栏高度 = 胶囊下距离 + 胶囊上距离 - 状态栏高度
					customBar = menu.bottom + menu.top - statusBar
					// #endif
					// #ifdef MP-ALIPAY
					customBar = statusBar + e.titleBarHeight
					// #endif
					this.$store.commit("SET_STATUSBAR", {
						statusBar: statusBar + compensateHeight,
						customBar: customBar + compensateHeight,
						screenHeight: e.screenHeight
					})
				}
			})

			this.initialization();
		},
		onShow: function() {
			// #ifdef  APP-PLUS	
			this.timers = setInterval(() => {
				this.checkUpdate();
			}, 2000);
			// #endif
		},
		methods: {
			// 初始化 im
			initialization() {
				// 初始化tim
				uni.$TUIKit = TIM.create({
					SDKAppID: constant.imApppId
				});
				wx.$TUIKit = uni.$TUIKit;

				// #ifdef  H5
				uni.$TUIKit.registerPlugin({
					'cos-js-sdk': COS
				});
				// #endif

				// #ifdef  APP-PLUS || MP-WEIXIN
				uni.$TUIKit.registerPlugin({
					'cos-wx-sdk': COS
				});
				// #endif
				uni.$TUIKitTIM = TIM;
				uni.$TUIKitEvent = TIM.EVENT;
				uni.$TUIKitVersion = TIM.VERSION;
				uni.$TUIKitTypes = TIM.TYPES; // 监听系统级事件
				uni.$resetLoginData = this.resetLoginData();

				uni.$TUIKit.on(uni.$TUIKitEvent.KICKED_OUT, this.onKickedOut);
				uni.$TUIKit.on(uni.$TUIKitEvent.SDK_READY, this.onSDKReady);

				// 是否登录
				this.isLogin();
			},
			isLogin() {
				const token = uni.getStorageSync('token');
				const userTxCode = uni.getStorageSync('userTxCode');
				if (token && userTxCode) {
					const userSig = genTestUserSig(userTxCode.toString()).userSig;
					uni.$TUIKit.login({
							userID: userTxCode.toString(),
							userSig: userSig
						})
						.then(res => {
							console.log('IM登录成功', res);
						})
						.catch(() => {
							console.log('IM登录失败', res);
						});
				} else {
					uni.reLaunch({
						url: '/im/login/login'
					});
				}
			},
			// TODO:
			resetLoginData() {
				this.globalData.expiresIn = '';
				this.globalData.sessionID = '';
				this.globalData.userInfo = {
					userID: '',
					userSig: '',
					token: '',
					phone: ''
				};
				this.globalData.userProfile = null;
			},
			onSDKReady() {
				this.$store.commit('login');
			},
			onKickedOut() {
				uni.reLaunch({
					url: '/im/login/login',
					success() {
						uni.showToast({
							title: '您被踢下线',
							icon: 'error'
						});
					}
				});
			},
			requestBaseUrl() {
				uni.request({
					url: 'http://api.ptszyxx.com/api/white/reqAddress',
					success: (res) => {
						if (200 == res.statusCode) {
							// constant.baseUrl = res.data.data.url;
						}
					}
				});
			},
			checkUpdate() {
				// #ifdef  APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (info) => {
					this.versionName = info.version;
				});
				let now = new Date().getTime();
				let urlstr = '';
				// #ifdef APP-ANDROID
				urlstr = "https://zhiyuapp.oss-cn-fuzhou.aliyuncs.com/uniapp_android/lalaqConfig-hw.json?t=" + now;//android
				// #endif
				// #ifdef APP-IOS
				urlstr = "https://zhiyuapp.oss-cn-fuzhou.aliyuncs.com/uniapp/lalaqConfig.json?t=" + now;//ios
				// #endif

				uni.request({
					url: urlstr,
					success: (res) => {
						clearInterval(this.timers);
						let isaliPay = res.data.isaliPay; //isaliPay 1为开启
						let vipManage = res.data.vipManage; // 1 关闭门票
						let ticketPrice = res.data.ticketPrice; //门票价格
						let wxappid = res.data.wxappid;
						let secret = res.data.secret;
						let xiaochengpay = res.data.xiaochengpay;
						if(res.data.ticketTime){
							this.globalData.ticketTotalTime = res.data.ticketTime
						}
						console.log(vipManage + '****************' + isaliPay + 'ticketPrice:' + ticketPrice);
						uni.setStorageSync('isaliPay', isaliPay);
						uni.setStorageSync('vipManage', vipManage);
						uni.setStorageSync('ticketPrice', ticketPrice);
						uni.setStorageSync('wxappid', wxappid);
						uni.setStorageSync('secret', secret);
						uni.setStorageSync('xiaochengpay', xiaochengpay);
						if (res.data.open == "1" && res.data.vss != this.versionName) {
							this.downloadWgt(res.data.wgtUrl);
						}
					}
				})
				// #endif
			},

			//wgt下载安装
			downloadWgt(wgtUrl) {
				if (plus) {
					plus.downloader.createDownload(wgtUrl, {
						filename: "_doc/update/"
					}, function(d, status) {
						if (status == 200) {
							plus.runtime.install(d.filename, {
								force: true
							}, function() {
								plus.nativeUI.closeWaiting();
								plus.runtime.restart();
							}, function(e) {
								plus.nativeUI.closeWaiting();
							});
						} else {
							plus.nativeUI.closeWaiting();
						}
					}).start();
				}
			},
			
			toTicket(){
				if(0 == this.globalData.ticketTime){
					let timeId = setInterval(()=>{
						console.log('ticket--start', this.globalData.ticketTime);
						this.globalData.ticketTime++
						if(this.globalData.ticketTime == this.globalData.ticketTotalTime){
							clearInterval(timeId)
							uni.setStorageSync("ticketOver"+uni.getStorageSync('userId'), true)
							this.globalData.ticketTime = 0
							this.globalData.openTicket = true
							uni.reLaunch({
								url: '/pages/m-pages/m-home/dazi',
							});
						}
					}, 1000)
				}
			}
		}
	};
</script>

<style lang="scss">
	@import 'uview-ui/index.scss';
	@import 'static/css/index.css';
	@import 'static/css/globa.scss';

	.filter-page {
		position: relative;
	}

	.filter-page::before {
		content: '';
		background-image: url('@/static/image/background.png');
		position: absolute;
		top: 0rpx;
		right: 0rpx;
		bottom: 0rpx;
		left: 0rpx;
		background-size: cover;
		filter: blur(10rpx);
		overflow: hidden;
		z-index: -1;
		transform: scale(1.1);
	}



	.c-fff {
		color: #fff;
	}
</style>
<style>
	page {
		position: relative;
		min-height: 100vh;
		overflow: hidden;
	}

	page::before {
		content: '';
		background-image: url('@/static/image/background.png');
		position: absolute;
		top: 0rpx;
		right: 0rpx;
		bottom: 0rpx;
		left: 0rpx;
		background-size: cover;
		background-clip: padding-box;
		filter: blur(16rpx);
		overflow: hidden;
		background-blend-mode: darken;
		z-index: -1;
		transform: scale(1.1);
	}
</style>